﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ZkSurvey.Common.SurveyMgr
{
    public class Survey : ModelBase
    {
        /// <summary>
        /// 主键Id
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// 问卷题目
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 问卷说明
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 问卷开始时间
        /// </summary>
        public DateTime StartTime { get; set; }

        /// <summary>
        /// 问卷结束时间
        /// </summary>
        public DateTime EndTime { get; set; }

        /// <summary>
        /// 问卷创建人
        /// </summary>
        public string Creator { get; set; }

        /// <summary>
        /// 问卷创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 问卷验证码
        /// </summary>
        /// <remarks>防止任意填写问卷，url验证</remarks>
        public string ValidationCode { get; set; }

        /// <summary>
        /// 问卷编码
        /// </summary>
        /// <remarks>唯一标记问卷，用于问卷访问url</remarks>
        public Guid Code { get; set; }

        //public List<Question> Questions { get; set; }

        public SurveyStatus Status
        {
            get
            {
                if (StartTime < DateTime.Now && EndTime > DateTime.Now)
                    return SurveyStatus.Running;
                else if (StartTime > DateTime.Now)
                    return SurveyStatus.NotStarted;
                else
                    return SurveyStatus.Completed;
            }
        }

        public string StatusTitle
        {
            get
            {
                if (Status == SurveyStatus.NotStarted)
                {
                    return "未开始";
                }
                else if (Status == SurveyStatus.Running)
                {
                    return "进行中";
                }
                else if (Status == SurveyStatus.Completed)
                {
                    return "已结束";
                }
                else
                    throw new ApplicationException("问卷状态超出规定范围");
            }
        }

        #region 验证
        protected override bool Validation()
        {
            if (string.IsNullOrEmpty(Title))
            {
                ValidationMessage = "问卷题目不能留空";
                return false;
            }
            if (string.IsNullOrEmpty(ValidationCode))
            {
                ValidationMessage = "问卷验证码不存在";
                return false;
            }
            if (Code.Equals(Guid.Empty))
            {
                ValidationMessage = "问卷编码不存在";
                return false;
            }
            if (StartTime.Equals(DateTime.MinValue))
            {
                ValidationMessage = "问卷开始时间不能留空";
                return false;
            }
            if (EndTime.Equals(DateTime.MinValue))
            {
                ValidationMessage = "问卷结束时间不能留空";
                return false;
            }
            if (StartTime > EndTime)
            {
                ValidationMessage = "问卷开始时间不能晚于结束时间";
                return false;
            }
            return true;
        }
        #endregion
    }
    public enum SurveyStatus
    {
        NotStarted = 1,
        Running = 2,
        Completed = 3
    }
    public static class SurveyListExtension
    {
        public static int RunningCount(this List<Survey> surveys)
        {
            return surveys.Count(s => s.Status == SurveyStatus.Running);
        }

        public static int CompletedCount(this List<Survey> surveys)
        {
            return surveys.Count(s => s.Status == SurveyStatus.Completed);
        }

        public static int NotStartdCount(this List<Survey> surveys)
        {
            return surveys.Count(s => s.Status == SurveyStatus.NotStarted);
        }
    }
}
